import {
	defineStore
} from 'pinia'
import {
	ref,
	computed
} from 'vue'
import piniaPersistConfig from "@/utils/persist"
import {
	dataSerialize
} from "@/utils/md5/index.js"
import {
	useUserStore
} from "@/store/user.js"
import {
	useAppStore
} from "@/store/app.js"
import {
	openMiniProgram
} from '@/common/common'
export const useWebviewStore = defineStore('webview', () => {
	const userStore = useUserStore()
	const appStore = useAppStore()
	// webview默认地址
	const default_url = ref('')
	// webview页面地址
	const url = ref('')
	// webview页面路径
	const page = ref('')
	// webview页面参数
	const params = ref('')
	// H5 页面路径+参数
	const sharePath = ref('')
	// webview页面分享参数
	const share = ref({})

	//修改H5路径
	const setSharePath = (data) => {
		sharePath.value = data
	}

	//修改分享参数
	const setShare = (data) => {
		share.value = data
	}

	//webview跳转方法
	const route = (data = {
		url: '',
		page: '',
		params: {}
	}) => {

		url.value = data.url || default_url.value

		page.value = data.page || `/pages/index/index`

		params.value = `?` + dataSerialize({
			...data.params
		}) + `isWebview=${1}`

		uni.navigateTo({
			url: '/pages/webview/index'
		})
	}

	//分享封装
	// const onShareAppMessage = () => {
	// 	const shareData = share.value
	// 	console.log(shareData, 'webview分享参数')
	// 	return shareData
	// }
	// H5端监听通信
	const onH5Message = (event) => {
		const {
			data,
			origin
		} = event;
		if (origin == default_url.value) {
			if (data?.data?.arg) {
				const message = data.data.arg
				if (message.type == 'route') {
					console.log(message, 'openMiniProgram的route类型')
					let method = message.method || 'reLaunch'
					switch (method) {
						case 'navigateTo':
							uni.navigateTo({
								url: message.url
							})
							break;
						case 'redirectTo':
							uni.redirectTo({
								url: message.url
							})
							break;
						case 'switchTab':
							uni.switchTab({
								url: message.url
							})
							break;
						case 'reLaunch':
							uni.reLaunch({
								url: message.url
							})
							break;
						default:
							break;
					}
				}

				// h5拉起小程序
				if (message.type == 'openMiniProgram') {
					console.log(message, 'openMiniProgram的实时信息')
					openMiniProgram(message.miniId, message.miniPath)
				}
			}
		}
	};


	// 非H5端 监听通信
	const onMessage = (data) => {
		// 公共处理，整理收到的消息
		const last_mesage = data.detail.data[data.detail.data.length - 1]

		// #ifdef MP-WEIXIN
		// 设置分享
		let shareList = data.detail.data.filter(e => {
			return e.type == 'share'
		})
		if (shareList.length) {
			const lastPageData = shareList[shareList.length - 1]
			appStore.redirect =
				`/pages/webview/index?url=${encodeURIComponent(lastPageData.url)}&path=${encodeURIComponent(lastPageData.path)}`
			console.log(appStore.redirect, '设置webview重定向')

			url.value = lastPageData.url
			setSharePath(lastPageData.path)
			setShare({
				title: appStore.applyName,
				path: `/pages/webview/index?url=${encodeURIComponent(url.value)}&path=${encodeURIComponent(sharePath.value)}`,
				imageUrl: '',
				desc: '',
				content: '',
			})
			console.log(share.value, 'H5传来的分享参数')

		}
		// #endif


		// APP端的处理
		// #ifdef APP
		// 设置App中 webview跳转回App
		if (last_mesage.type == 'route') {
			const method = last_mesage.method || 'reLaunch'
			uni[method]({
				url: last_mesage.url
			})
		}
		// App拉起小程序
		if (last_mesage.type == 'openMiniProgram') {
			console.log(last_mesage, 'openMiniProgram的实时信息')
			openMiniProgram(last_mesage.miniId, last_mesage.miniPath)
		}
		// #endif

	}

	// return 仓库数据，外部调用
	return {
		share,
		sharePath,
		setSharePath,
		setShare,
		// onShareAppMessage,
		onMessage,
		onH5Message,

		default_url,
		url,
		page,
		params,
		route,
	}
}, {
	persist: piniaPersistConfig('webviewStore')
})